/**********************************************************************************
 * 文件名称：
 * 功能说明：
 * 作者：laud（554163925@qq.com）
 * 修改记录
 *
**********************************************************************************/
#include "gsm.h"
#include "gsm_modem.h"
#include "gsm_utility.h"


/* 
 * 功能描述: 命令拷贝
 * 引用参数: (1)目标指令
 *           (2)原始指令
 *
 * 返回值  : 状态码
 * 
 */
extern bool MDM_CmdCpy ( MDM_CmdTypedef *DstCmd, MDM_CmdTypedef *SrcCmd )
{
  if ( DstCmd && SrcCmd )
  {
    DstCmd->cmd_id = SrcCmd->cmd_id;        
    DstCmd->timeout = SrcCmd->timeout;
    DstCmd->retry_counter = SrcCmd->retry_counter;
    DstCmd->handler = SrcCmd->handler;
    DstCmd->required_state = SrcCmd->required_state;
    DstCmd->write_complete = SrcCmd->write_complete;
    if ( DstCmd->AtBuffer && SrcCmd->AtBuffer )
    {
      strcpy ( ( char * )DstCmd->AtBuffer, ( const char * )SrcCmd->AtBuffer );
    }
    return true;    
  } 
  return false; 
}

/* 
 * 功能描述: 命令设置
 * 引用参数:
 *          
 * 返回值  :
 * 
 */
extern bool MDM_CmdSet ( MDM_CmdTypedef *SrcCmd )
{
    if ( SrcCmd != NULL )
    {
        SrcCmd->cmd_id = 0;
        SrcCmd->timeout = 0;
        SrcCmd->retry_counter = 0;
        SrcCmd->handler = NULL;
        SrcCmd->required_state = SIM_UNKNOWN;
        SrcCmd->write_complete = false;
        if ( SrcCmd->AtBuffer )
        {
        memset ( ( char * )SrcCmd->AtBuffer, 0, strlen ( ( const char * )SrcCmd->AtBuffer ) );
        }
        return true;    
    } 
    return false; 
}

/* 
 * 功能描述: 字符串查找
 * 引用参数:
 *          
 * 返回值  :
 * 
 */
extern ascii * MDM_StrStr ( MDM_RspTypedef *response, ascii *key )
{
  if ( response && key )
  {
    ascii *token = response->indicator;
    return token = ( ascii* )strstr ( ( const char * )token, ( const char * )key );
  } 
  return NULL; 
}

/* 
 * 功能描述: 符号映射为字符串
 * 引用参数:
 *          
 * 返回值  :
 * 
 */

ascii * MDM_Sym2Str ( const SymbolTable *symbolbase, int32_t token )
{
  const SymbolTable *symbol = symbolbase;

  if ( symbol && token > 0 )
  {
    while ( symbol )
    {
      if ( symbol->symbol_token == token )
      {
        return symbol->symbol_name; 
      }
      else
      {
        symbol++; 
      }
    }
  }
   
  return NULL; 
}


/* 
 * 功能描述: 获取响应ID
 * 引用参数:
 *          
 * 返回值  :
 * 
 */
extern MDM_RSP_ID MDM_GetResultCode ( MDM_RspTypedef *response )
{
    if ( response )
    {
        return response->Code;
    }
    else
    {
        return MDM_NO_STRING;
    }
}


extern MDM_RSP_ID MDM_StrGetID ( MDM_RspTypedef *response )
{
    uint8_t *info = ( uint8_t* )( response->indicator );
    uint16_t size = ( uint16_t )( response->totalsize );

    if ( info == NULL )
    {
        return  MDM_NO_STRING;
    }
    else if ( MDM_GetDataStr ( info, size, "CONNECT" ) == true )
    {
        return  MDM_CONNECT;
    }
    else if ( MDM_GetDataStr ( info, size, "SEND OK" ) == true )
    {
        return  MDM_SEND_OK;
    }
    else if ( MDM_GetDataStr ( info, size, "OK" ) == true )    
    {
        return  MDM_OK;
    }
    else if ( MDM_GetDataStr ( info, size, "BUSY" ) == true )
    {
        return  MDM_BUSY;
    }
    else if ( MDM_GetDataStr ( info, size, "NO ANSWER" ) == true )
    {
        return  MDM_NO_ANSWER;
    }
    else if ( MDM_GetDataStr ( info, size, "NO CARRIER" ) == true )
    {
        return  MDM_NO_CARRIER;
    }
    else if ( MDM_GetDataStr ( info, size, "ERROR" ) == true )
    {
        return  MDM_ERROR;
    }
    else if ( MDM_GetDataStr ( info, size, "+CME ERROR:" ) == true )
    {
        return  MDM_CME_ERROR;
    }
    else if ( MDM_GetDataStr ( info, size, "+CMS ERROR:" ) == true )
    {
        return  MDM_CMS_ERROR;
    }
    else if ( MDM_GetDataStr ( info, size, "+CPIN:" ) == true )
    {
        return  MDM_CPIN;
    }
    else if ( MDM_GetDataStr ( info, size, "RING" ) == true )
    {
        return  MDM_RING;
    }
    else if ( MDM_GetDataStr ( info, size, "+CRING:" ) == true )
    {
        return  MDM_CRING;
    }
    else if ( MDM_GetDataStr ( info, size, "+CMEE:" ) == true )
    {
        return  MDM_CMEE;
    }
    else if ( MDM_GetDataStr ( info, size, "+CREG:" ) == true )
    {
        return  MDM_CREG;
    }
    else if ( MDM_GetDataStr ( info, size, "+CGREG:" ) == true )
    {
        return  MDM_CGREG;
    }
    else if ( MDM_GetDataStr ( info, size, "+CRC:" ) == true )
    {
        return  MDM_CRC;
    }
    else if ( MDM_GetDataStr ( info, size, "+CGEV:" ) == true )
    {
        return  MDM_CGEV;
    }
    else if ( MDM_GetDataStr ( info, size, "+CGEREP:" ) == true )
    {
        return  MDM_CGEREP;
    }
    else if ( MDM_GetDataStr ( info, size, "\r\n>" ) == true )
    {
        return  MDM_SEND_QUERY_OK;
    }
//    else if ( MDM_GetDataStr ( info, size, MDM_Sym2Str ( symbol_p, MDM_SEND_OK ) ) == true )
//    {
//        return  MDM_SEND_OK;
//    }
//    else if ( MDM_GetDataStr ( info, size, MDM_Sym2Str ( symbol_p, MDM_RECEIVE ) ) == true )
//    {
//        return  MDM_RECEIVE;
//    }
//    else if ( MDM_GetDataStr ( info, size, MDM_Sym2Str ( symbol_p, MDM_CIPOPEN ) ) == true )
//    {
//        return  MDM_CIPOPEN;
//    }
    else
    {
        return  MDM_NO_STRING;
    }
}


/* 功能描述: 是最终响应？*/
extern bool MDM_StrIsTerminalResponse ( MDM_RSP_ID RspID )
{
  return ( ( RspID >= MDM_OK )&&( RspID < MDM_LAST_TERMINAL ) ) ? true : false;
}


/* 
 * 功能描述: 二进制数据中查找特定字符串
 * 引用参数:
 *          
 * 返回值  :
 * 
 */
bool MDM_GetDataStr ( uint8_t* Data, uint16_t DataSize, ascii *String )
{
    if ( Data && DataSize > 0 && String )
    {
        int32_t length = strlen ( ( const char* )String );
        int32_t i = 0;
        int32_t j = 0;
        uint8_t *found = NULL;

        /* ADD #001 初始条件过滤 */
        if ( length <= 0 && DataSize < length )
        {
            return false;
        }

        for ( j = 0; j < length; )
        {
            /* 设定预查找的字符 */
            found = ( uint8_t* )( String + j );

            /* 在数据源中查找 */
            for ( ; i < DataSize; )
            {
                /* 比较成功，则进行下一轮比较 */
                if ( *found == *( Data + i ) )
                {
                    j++;

                    /* BUG #001 比较下一个目标 */
                    i++;
                    break;
                }
                else
                {
                    /* BUG #002 比较失败时重头开始比较 */
                    if ( j == 0 )
                    {
                    i++;
                    }
                    j = 0;
                    break;
                }
            }

            /* 特定字符查找失败，则退出 */
            if ( i >= DataSize && j < length )
            {
                return false;
            }
        }

        if ( j >= length )
        {
            return true;
        }
    }

    return false;
} 

